<template>
  <div class="navBarContainer">
    <!--		<div class="navBar" :style="'background-color:rgba(149, 164, 255,' + backgroundAlpha + ')'">-->
    <div class="navBar" :style="'background-color:rgba(24, 54, 91,' + backgroundAlpha + ')'">
      <div class="left">
        <p class="mylogo" @click="goCurrentPage('/home')">河北工业大学非毕业生就业系统</p>
        <div class="nav">
          <!--					<div class="navItem" @click="goCurrentPage('/home')">主页</div>-->
          <div class="navItem" @click="goCurrentPage('/community1?typeId=0&page=1')">分享区</div>
          <div class="navItem" @click="goCurrentPage('/community2?typeId=0&page=1')">问答区</div>
          <div class="navItem" @click="goCurrentPage('/interview?page=1')">招聘区</div>
          <div class="navItem" @click="goCurrentPage('/types?page=1')" v-if="$store.state.userInfo.role=='超级管理员'">分类管理</div>
          <div class="navItem" @click="goCurrentPage('/users?page=1')" v-if="$store.state.userInfo.role=='超级管理员'">用户管理</div>
        </div>
      </div>
      <div class="right">
        <!-- 搜索组件 -->
        <search-input></search-input>
        <div class="fenge"></div>
        <!-- 用户信息 -->
        <div class="user">
          <div class="avatar" @click="gotoPersonal">
            <el-image :src="userInfo.avatar? userInfo.avatar: require('assets/img/defaultAvatar.jpg')"
                      fit="cover"></el-image>
          </div>
          <div class="userName">{{ userInfo.nickName }}</div>
        </div>
      </div>
    </div>
    <div class="placeHolder" v-if="!isAlphaChange"></div>
  </div>
</template>

<script>
import SearchInput from "components/searchInput/SearchInput.vue";

export default {
  components: {
    SearchInput
  },
  name: "navBar",
  props: {
    isAlphaChange: {
      type: Boolean,
      default() {
        return false;
      },
    },
  },
  data() {
    return {
      // 背景色透明度
      backgroundAlpha: 1,
      // 用户信息
      userInfo: {},
    };
  },
  methods: {
    // 请求
    // 请求用户信息
    async getUserInfo() {
      // 判断是否有token 没有token则不获取用户信息
      // if (!window.localStorage.getItem("tokenValue")) {
      //   return;
      // }

      let res = await this.$request("/dquser/getnowuser");
      // console.log(res);
      // 判断token是否过期或失效
      if (res.data.code != 200) {
        // 如果过期或失效 清空token 并return
        // window.localStorage.removeItem("tokenValue");
        this.userInfo = {};
        this.$store.commit("updateUserInfo", {});
        // this.$message.info("登录信息失效, 请重新登录!");
        return;
      }

      this.userInfo = res.data.data;
      this.$store.commit("updateUserInfo", this.userInfo);
    },

    // 事件
    // 点击navItem 的回调
    goCurrentPage(path) {
      if (this.$route.fullPath != path) {
        this.$router.push(path);
        // 回到顶部
        window.scrollTo(0, 0);
      }
    },

    // 跳转至个人页面
    gotoPersonal() {
      // 判断是否登录
      // 通过查看vuex中的userInfo是否为空来判断是否登录
      if (!this.$store.state.userInfo.userId) {
        // 跳转至登录界面
        this.$router.push("/login");
        return;
      }

      if (this.$route.path == `/personal/${this.userInfo.userId}`) return;
      this.$router.push({
        name: "personal",
        params: {
          id: this.userInfo.userId
        },
        query: {
          type: "post",
          page: 1
        },
      });
    },
  },
  created() {
    this.getUserInfo();
  },
  mounted() {
    this.pageHeight = document.body.clientHeight;

    // 判断是否需要根据高度动态改变navbar的背景色
    // console.log(123);
    this.backgroundAlpha = this.isAlphaChange ? 0 : 1;

    // 监听页面滚动
    window.addEventListener("scroll", () => {
      if (this.isAlphaChange) {
        this.backgroundAlpha = 0;
        // 有可能滑下去后透明度还没到 1，这里如果判断到已经滑下去但透明度还没到1 则直接置为 1
        if (window.scrollY <= document.body.clientHeight) {
          this.backgroundAlpha = window.scrollY / document.body.clientHeight;
          // console.log(this.backgroundAlpha);
        } else {
          if (this.backgroundAlpha < 1) {
            this.backgroundAlpha = 1;
          }
        }
      }
    });
  },
  watch: {
    isAlphaChange(current) {
      this.backgroundAlpha = current ? 0 : 1;
    },

    // 监听是否需要重新获取用户信息
    "$store.state.reFreshUserInfo"(current) {
      if (current) {
        this.getUserInfo();
        this.$store.commit("updateReFreshUserInfo", false);
      }
    },
  },
};
</script>

<style scoped>
.navBar {
  display: flex;
  position: fixed;
  color: white;
  align-items: center;
  justify-content: space-between;
  background-color: rgb(24, 54, 91);
  width:1526px;
  z-index: 3000;
  height: 74px;
}

.left,
.nav {
  display: flex;
  align-items: center;
}
.nav{
  margin-left: 100px;
}

.logo,
.navItem,
.button {
  margin: 20px;
  cursor: pointer;
  user-select: none;
  /*font-weight: bold;*/
  font-size: 20px;
  font-style: italic;
}

.logo {
  margin: 20px 40px 20px 30px;
}

.user {
  margin: 20px 20px 20px 0px;
  display: flex;
  align-items: center;
  width: 130px;
}

.userName {
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.left img {
  width: 150px;
}

.placeHolder {
  height: 74px;
}

.avatar .el-image {
  height: 40px;
  width: 40px;
  border-radius: 50%;
}

.avatar {
  margin-right: 10px;
  cursor: pointer;
}

.left {
  margin-left: 10px;
}

.right {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-right: 100px;
}

.fenge {
  height: 18px;
  width: 2px;
  border-radius: 1px;
  /* 圆角边框 */
  background-color: rgba(255, 255, 255, 0.4);
  margin: 20px;
}
.mylogo{
  font-weight: bold;
  font-family: "YouYuan" ;
  font-size: 30px;
  cursor: pointer;
}
</style>
